package patrol.mobile.com.mobilepatrol.activity.toll;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.adapter.SummaryOfChargesAdapter;
import patrol.mobile.com.mobilepatrol.adapter.SummaryOfChargesAdapter2;
import patrol.mobile.com.mobilepatrol.adapter.TrafficAllAdapter;
import patrol.mobile.com.mobilepatrol.adapter.TrafficAllAdapter2;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.LineChartBean;
import patrol.mobile.com.mobilepatrol.bean.NewLineChartBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.bean.TrafficAllBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.LineChartUtil;
import patrol.mobile.com.mobilepatrol.utils.NewLineChartUtil;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

/**
 * 收费额汇总
 */
public class SummaryOfChargesActivity extends BaseActivity implements CallBack<DateBean> {

    @BindView(R.id.mySpinner01)
    MySpinner mySpinner01;
    @BindView(R.id.mLineChart)
    LineChart mLineChart;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    List<String> strings;
    TrafficAllAdapter mAdapter;
    @BindView(R.id.tv_start)
    TextView tvStart;
    @BindView(R.id.tv_end)
    TextView tvEnd;
    private int clickFlag;//点击选择时间的标志 1是开始时间 2是结束时间
    private String currentOrgId = "";
    private String currentStartTime = "";
    private String currentEndTime = "";
    private OrgListBean orgListBean;
    SummaryOfChargesAdapter2 adapter2;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_summary_of_charges;
    }

    @Override
    public void initView() {
        iv_back.setVisibility(View.VISIBLE);
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("收费额汇总");
        super.initView();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TrafficAllAdapter(this);
        adapter2 = new SummaryOfChargesAdapter2(this);
        mRecyclerView.setAdapter(adapter2);
        mRecyclerView.clearFocus();
    }

    @Override
    public void initData() {
        super.initData();
        getOrg();
    }

    private void requestData(String orgId, String startTime, String endTime) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.GET_TRAFFIC_TOTAL_QTY_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        map.put("startPeriod", startTime);
        map.put("endPeriod", endTime);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                List<TrafficAllBean> trafficAllBeans = GsonUtil.jsonToList(response, TrafficAllBean.class);
                if (null == trafficAllBeans || trafficAllBeans.size() == 0) {
                    ToastUtils.showToast("暂无数据");
                    return;
                }
                adapter2.setData(trafficAllBeans);
                List<NewLineChartBean> lineList = getNewLineChartBeans(trafficAllBeans);
                List<String> xList = getList(trafficAllBeans);
                NewLineChartUtil.setLineChart(mLineChart, lineList, xList);
            }
        });
    }

    @NonNull
    private List<String> getList(List<TrafficAllBean> trafficAllBeans) {
        List<String> xList = new ArrayList<>();
        for (TrafficAllBean trafficAllBean : trafficAllBeans) {
            xList.add(trafficAllBean.getUpDate());
        }
        return xList;
    }

    @NonNull
    private List<NewLineChartBean> getNewLineChartBeans(List<TrafficAllBean> trafficAllBeans) {
        List<NewLineChartBean> lineList = new ArrayList<>();
        NewLineChartBean newLineChartBean = new NewLineChartBean();
        newLineChartBean.setLineName("收费额");
        List<NewLineChartBean.YDataBean> yList = new ArrayList<>();
        for (TrafficAllBean trafficAllBean : trafficAllBeans) {
            NewLineChartBean.YDataBean yDataBean = new NewLineChartBean.YDataBean();
            yDataBean.setValue((long) trafficAllBean.getToll());
            yList.add(yDataBean);
        }
        newLineChartBean.setYBataBeanList(yList);

        NewLineChartBean newLineChartBean2 = new NewLineChartBean();
        newLineChartBean2.setLineName("减免金额");
        List<NewLineChartBean.YDataBean> yList2 = new ArrayList<>();
        for (TrafficAllBean trafficAllBean : trafficAllBeans) {
            NewLineChartBean.YDataBean yDataBean = new NewLineChartBean.YDataBean();
            yDataBean.setValue((long) trafficAllBean.getGreenDerateToll());
            yList2.add(yDataBean);
        }
        newLineChartBean2.setYBataBeanList(yList2);

        lineList.add(newLineChartBean);
        lineList.add(newLineChartBean2);
        return lineList;
    }

    @Override
    public void setListener() {
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mySpinner01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mySpinner01.setOnItemSelectedListener();
            }
        });
        tvStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickFlag = 1;
                DatePickerUtil.getInstance(SummaryOfChargesActivity.this).setCallBack(SummaryOfChargesActivity.this);
            }
        });
        tvEnd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickFlag = 2;
                DatePickerUtil.getInstance(SummaryOfChargesActivity.this).setCallBack(SummaryOfChargesActivity.this);
            }
        });
    }

    private void getOrg() {
        HashMap<String, String> hashMap = new HashMap();
//        hashMap.put("objType", "分局,总局");
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(final String code, String response) {
                orgListBean = new Gson().fromJson(response, OrgListBean.class);
                List<String> list = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    list.add(listBean.getOrgName());
                }
                mySpinner01.setmDatas(list, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        currentOrgId = orgListBean.getList().get(postion).getId();
                        requestData(currentOrgId, currentStartTime, currentEndTime);
                    }
                });
                if (orgListBean.getList().size() == 0) return;
                requestData(orgListBean.getList().get(0).getId(), currentStartTime, currentEndTime);
            }
        });
    }

    @Override
    public void onBack(DateBean dateBean) {
        if (clickFlag == 1) {
            currentStartTime = dateBean.getmDate();
            tvStart.setText(dateBean.getmDate());
        } else {
            currentEndTime = dateBean.getmDate();
            tvEnd.setText(dateBean.getmDate());
        }
        if (!CommonUtil.getText(tvStart).equals("开始时间") && !CommonUtil.getText(tvEnd).equals("结束时间") && orgListBean != null) {
            requestData(currentOrgId, currentStartTime, currentEndTime);
        }
    }
}
